<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/10/010
 * Time: 14:17
 */

namespace app\Lib\Model\Order;


use app\Lib\Model\DbModel\OrderLog;
use app\Lib\Model\DbModel\OrderLogFile;
use app\Lib\Model\Img\Img;

class GetOrderLog
{
    private $order;
    private $page = 1;
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    public function page($page ){
        $this->page = $page;
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function run(){
        $logModel =  new OrderLog();
        $count =  $logModel->where($logModel->_order_id,$this->order->get_order_id())->count();
        if($count <= 0){
            return [];
        }
        $orderLog = $logModel->where($logModel->_order_id,$this->order->get_order_id())
            ->order($logModel->_create_time.' desc')->limit(($this->page-1)*10,10)
            ->field([$logModel->_id,$logModel->_create_time,$logModel->_content])
            ->select();
        $newOrderLog = [];
        foreach ($orderLog as $k=>$v){
            $time = $v['create_time'];
            $content = $v['content'];
            $orderLogFile = new OrderLogFile();
            $imgArray = $orderLogFile->where($orderLogFile->_order_log_id,$v['id'])->column($orderLogFile->_img_id);
            $file = [];
            if($imgArray){
                foreach ($imgArray as $v){
                    $imgModel = new Img($v);
                    $file[] = $imgModel->getFilePath();
                }
            }
            $newOrderLog[$time] =[
                'msg'=>$content,
                'file'=>$file
            ];
        }
        return $newOrderLog;
    }
}